package com.liuxvru.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxvru.common.utils.SecurityUtils;
import com.liuxvru.project.domain.Flaw;
import com.liuxvru.project.domain.Test;
import com.liuxvru.project.domain.TestPlan;
import com.liuxvru.project.domain.TestStep;
import com.liuxvru.project.mapper.FlawMapper;
import com.liuxvru.project.mapper.TestMapper;
import com.liuxvru.project.mapper.TestPlanMapper;
import com.liuxvru.project.mapper.TestStepMapper;
import com.liuxvru.project.service.ITestPlanService;
import com.liuxvru.project.vo.TestPlanInitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 测试计划Service业务层处理
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@Service
public class TestPlanServiceImpl extends ServiceImpl<TestPlanMapper, TestPlan> implements ITestPlanService {

    @Autowired
    private TestMapper testMapper;
    @Autowired
    private TestStepMapper testStepMapper;
    @Autowired
    private FlawMapper flawMapper;

    /**
     * 查询测试计划列表
     *
     * @param testPlan 测试计划
     * @return 测试计划
     */
    @Override
    public List<TestPlan> selectTestPlanList(TestPlan testPlan) {
        return baseMapper.selectTestPlanList(testPlan);
    }

    @Override
    public int add(Long projectId, TestPlan testPlan) {
        Long userId = SecurityUtils.getUserId();
        testPlan.setCreatedBy(userId);
        testPlan.setUpdatedBy(userId);
        testPlan.setProjectId(projectId);
        return baseMapper.insert(testPlan);
    }

    /**
     * 获取详细信息
     * @param planId
     * @return
     */
    @Override
    public TestPlanInitVo allInit(Long planId) {
        TestPlanInitVo testPlanInitVo = new TestPlanInitVo();
        TestPlan testPlan = baseMapper.selectById(planId);
        List<Test> tests = testMapper.selectList(new LambdaQueryWrapper<Test>().eq(Test::getTestPlanId, planId));
        List<TestStep> testSteps = testStepMapper.selectList(new LambdaQueryWrapper<TestStep>().eq(TestStep::getTestPlanId, planId));
        List<Flaw> flaws = flawMapper.selectList(new LambdaQueryWrapper<Flaw>().eq(Flaw::getTestPlanId, planId));
        testPlanInitVo.setTestPlan(testPlan);
        testPlanInitVo.setTests(tests);
        testPlanInitVo.setTestSteps(testSteps);
        testPlanInitVo.setFlaws(flaws);
        return testPlanInitVo;
    }

}
